﻿using System;
using MediaMover.Common;
using MediaMover.Data.NHIbernate;
using MediaMover.DataAccess;

namespace MediaMover.Data.Repositories
{
    public class SettingsRepository : ISettingsRepository
    {
        public void Add(Settings settings)
        {
            using (var session = Helper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.Save(settings);
                    transaction.Commit();
                }
            }
        }

        public void Update(Settings settings)
        {
            using (var session = Helper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.SaveOrUpdate(settings);
                    transaction.Commit();
                }
            }
        }

        public void Remove(Settings settings)
        {
            using (var session = Helper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.Delete(settings);
                    transaction.Commit();
                }
            }
        }

        public Settings GetById(int settingsId)
        {
            using (var session = Helper.OpenSession())
            {
                return session.Get<Settings>(settingsId) ?? new Settings();
            }
        }
    }
}
